/*********************************************************************************
  *FileName:  nsuc16xx_bemfc.c
  *Author:    GuanChaoYang
  *Version:   V1.0
  *Date:      2024-03-04
****************************************Includes***********************************/
#include "nsuc16xx_bemfc.h"

/***************************************Variables***********************************/

/***************************************Functions***********************************/
/**
  * @brief  Enables or disables the specified BEMFC0 Channel.
  * @param  BEMFCx: Select the BEMFC peripheral.
  * @param  NewState: new state of the BEMFC peripheral.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void BEMFC0_Cmd(BEMFC_TypeDef *BEMFCx, FunctionalState NewState)
{
	if (NewState != DISABLE)
    {
        BEMFCx->CR0_b.EN0 = 1;
    }
    else
    {
        BEMFCx->CR0_b.EN0 = 0;
    }
}

/**
  * @brief  Enables or disables the specified BEMFC1 Channel.
  * @param  BEMFCx: Select the BEMFC peripheral.
  * @param  NewState: new state of the BEMFC peripheral.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void BEMFC1_Cmd(BEMFC_TypeDef *BEMFCx, FunctionalState NewState)
{
	if (NewState != DISABLE)
    {
        BEMFCx->CR0_b.EN1 = 1;
    }
    else
    {
        BEMFCx->CR0_b.EN1 = 0;
    }
}

/**
  * @brief  Enables or disables the specified BEMFC2 Channel.
  * @param  BEMFCx: Select the BEMFC peripheral.
  * @param  NewState: new state of the BEMFC peripheral.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void BEMFC2_Cmd(BEMFC_TypeDef *BEMFCx, FunctionalState NewState)
{
	if (NewState != DISABLE)
    {
        BEMFCx->CR0_b.EN2 = 1;
    }
    else
    {
        BEMFCx->CR0_b.EN2 = 0;
    }
}

/**
  * @brief  Initializes the BEMFC0 peripheral according to the specified parameters
  *         in the BEMFC_InitStruct.
  * @param  BEMFCx: Select the BEMFC peripheral.
  * @param  BEMFC_InitStruct: pointer to an BEMMFC_InitTypeDef structure that contains
  *         the configuration information for the specified BEMFC0 peripheral.
  * @retval None
  */
void BEMFC0_Init(BEMFC_TypeDef *BEMFCx, BEMFC_InitTypeDef *BEMFC_InitStruct)
{
	BEMFCx->CR0_b.DGL0 = BEMFC_InitStruct->BEMFC_FilterTime;
	BEMFCx->CR0_b.INV0 = BEMFC_InitStruct->BEMFC_OutDiretion;
	BEMFCx->CR0_b.BLKT0 = BEMFC_InitStruct->BEMFC_BlankingTime;
	BEMFCx->CR2_b.HYST0 = BEMFC_InitStruct->BEMFC_Delay;
	BEMFCx->CR2_b.BIS0 = BEMFC_InitStruct->BEMFC_Input;
	BEMFCx->CR1_b.DTS0 = BEMFC_InitStruct->BEMFC_EventSource;
	BEMFCx->CR1_b.DTP0 = BEMFC_InitStruct->BEMFC_EventTest;
}

/**
  * @brief  Initializes the BEMFC1 peripheral according to the specified parameters
  *         in the BEMFC_InitStruct.
  * @param  BEMFCx: Select the BEMFC peripheral.
  * @param  BEMFC_InitStruct: pointer to an BEMMFC_InitTypeDef structure that contains
  *         the configuration information for the specified BEMFC1 peripheral.
  * @retval None
  */
void BEMFC1_Init(BEMFC_TypeDef *BEMFCx, BEMFC_InitTypeDef *BEMFC_InitStruct)
{
	BEMFCx->CR0_b.DGL1 = BEMFC_InitStruct->BEMFC_FilterTime;
	BEMFCx->CR0_b.INV1 = BEMFC_InitStruct->BEMFC_OutDiretion;
	BEMFCx->CR0_b.BLKT1 = BEMFC_InitStruct->BEMFC_BlankingTime;
	BEMFCx->CR2_b.HYST1 = BEMFC_InitStruct->BEMFC_Delay;
	BEMFCx->CR2_b.BIS1 = BEMFC_InitStruct->BEMFC_Input;	
	BEMFCx->CR1_b.DTS1 = BEMFC_InitStruct->BEMFC_EventSource;
	BEMFCx->CR1_b.DTP1 = BEMFC_InitStruct->BEMFC_EventTest;	
}

/**
  * @brief  Initializes the BEMFC2 peripheral according to the specified parameters
  *         in the BEMFC_InitStruct.
  * @param  BEMFCx: Select the BEMFC peripheral.
  * @param  BEMFC_InitStruct: pointer to an BEMMFC_InitTypeDef structure that contains
  *         the configuration information for the specified BEMFC2 peripheral.
  * @retval None
  */
void BEMFC2_Init(BEMFC_TypeDef *BEMFCx, BEMFC_InitTypeDef *BEMFC_InitStruct)
{
	BEMFCx->CR0_b.DGL2 = BEMFC_InitStruct->BEMFC_FilterTime;
	BEMFCx->CR0_b.INV2 = BEMFC_InitStruct->BEMFC_OutDiretion;
	BEMFCx->CR0_b.BLKT2 = BEMFC_InitStruct->BEMFC_BlankingTime;
	BEMFCx->CR2_b.HYST2 = BEMFC_InitStruct->BEMFC_Delay;
	BEMFCx->CR2_b.BIS2 = BEMFC_InitStruct->BEMFC_Input;	
	BEMFCx->CR1_b.DTS2 = BEMFC_InitStruct->BEMFC_EventSource;
	BEMFCx->CR1_b.DTP2 = BEMFC_InitStruct->BEMFC_EventTest;	
}

/**
  * @brief  Initializes the BEMFC peripheral according to the specified parameters
  *         in the Reference_InitStruct.
  * @param  BEMFCx: Select the BEMFC peripheral.
  * @param  Reference_InitStruct: pointer to an Reference_InitTypeDef structure that contains
  *         Reference configuration of BEMFC.
  * @retval None
  */
void BEMFC_Congfig(BEMFC_TypeDef *BEMFCx, Reference_InitTypeDef *Reference_InitStruct)
{
	BEMFCx->CR2_b.BCRM = Reference_InitStruct->Current_Reference;
	BEMFCx->CR2_b.BRM = Reference_InitStruct->Reference_Mode;
	BEMFCx->CR3_b.BCSS0 = Reference_InitStruct->Channel0_Current_Source;
	BEMFCx->CR3_b.BCSS1 = Reference_InitStruct->Channel1_Current_Source;
}

/**
  * @brief  If caching mode is enabled ,the corresponding registers are matched
			Write access is redirected to its cache register, so it does not immediately affect the control signal. 
			Only in update events (BEMFC_UPDATE And /or CLDACx_UPDATE) before the contents of the cache register are 
			transferred to the configuration register.
  * @param  BEMFCx: Select the BEMFC peripheral.
  * @param  NewState: new state of the BEMFC peripheral.
  *   		This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void CLDAC_TRG0_EeventCmd(BEMFC_TypeDef *BEMFCx, FunctionalState NewState)
{
	if (NewState != DISABLE)
    {
        BEMFCx->CLDAC_BCR_b.CLUTRG = 1;
    }
    else
    {
        BEMFCx->CLDAC_BCR_b.CLUTRG = 0;
    }
}

/**
  * @brief  If caching mode is enabled ,the corresponding registers are matched
			Write access is redirected to its cache register, so it does not immediately affect the control signal. 
			Only in update events (BEMFC_UPDATE And /or CLDACx_UPDATE) before the contents of the cache register are 
			transferred to the configuration register.
  * @param  BEMFCx: Select the BEMFC peripheral.
  * @param  NewState: new state of the BEMFC peripheral.
  *   		This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void CLDAC_EOP0_EeventCmd(BEMFC_TypeDef *BEMFCx, FunctionalState NewState)
{
	if (NewState != DISABLE)
    {
        BEMFCx->CLDAC_BCR_b.CLUEOP = 1;
    }
    else
    {
        BEMFCx->CLDAC_BCR_b.CLUEOP = 0;
    }
}

/**
  * @brief  If caching mode is enabled ,the corresponding registers are matched
			Write access is redirected to its cache register, so it does not immediately affect the control signal. 
			Only in update events (BEMFC_UPDATE And /or CLDACx_UPDATE) before the contents of the cache register are 
			transferred to the configuration register.
  * @param  BEMFCx: Select the BEMFC peripheral.
  * @param  NewState: new state of the BEMFC peripheral.
  *   		This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void CLDAC_BufferCmd(BEMFC_TypeDef *BEMFCx, FunctionalState NewState)
{
	if (NewState != DISABLE)
    {
        BEMFCx->CLDAC_BCR_b.CLBE = 1;
    }
    else
    {
        BEMFCx->CLDAC_BCR_b.CLBE = 0;
    }
}

/**
  * @brief  Enables or disables the specified CLDAC peripheral.
  * @param  BEMFCx: Select the BEMFC peripheral.
  * @param  NewState: new state of the BEMFC peripheral.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void CLDAC_Cmd(BEMFC_TypeDef *BEMFCx, FunctionalState NewState)
{
		if (NewState != DISABLE)
    {
        BEMFCx->CLDAC_CR_b.EN = 1;
    }
    else
    {
        BEMFCx->CLDAC_CR_b.EN = 0;
    }
}

/**
  * @brief  CLDAC channel 0 current limit.
  * @param  BEMFCx: Select the BEMFC peripheral.
  * @param  HARD_CURRENT_LIMIT_SET: Current limiting value
  * @retval None
  */
void CLDAC0_Current_Limit(BEMFC_TypeDef *BEMFCx,uint16_t HARD_CURRENT_LIMIT_SET)
{
	BEMFCx->CLDAC_DR_b.DATA0 = HARD_CURRENT_LIMIT_SET;
}

/**
  * @brief  CLDAC channel 1 current limit.
  * @param  BEMFCx: Select the BEMFC peripheral.
  * @param  HARD_CURRENT_LIMIT_SET: Current limiting value
  * @retval None
  */
void CLDAC1_Current_Limit(BEMFC_TypeDef *BEMFCx,uint16_t HARD_CURRENT_LIMIT_SET )
{
	BEMFCx->CLDAC_DR_b.DATA1 = HARD_CURRENT_LIMIT_SET;
}
